package com.hsc.mvc.handler;

import com.hsc.mvc.annotations.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author
 * @date 2025-06-10
 * @Description 字符串直接响应器
 * 直接响应的意思是  response.getWriter().write((String));
 */
public class StringResultHandler implements ResponseHandleStrategy{
    /**
     * 字符串直接响应
     * @param result
     * @param targetMethod
     * @return
     */
    @Override
    public boolean isSupport(Object result, Method targetMethod) {
        return result instanceof String && targetMethod.isAnnotationPresent(ResponseBody.class);
    }

    @Override
    public void handle(Object result, Method targetMethod, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        // 处理响应接口 直接流写回给浏览器
        response.getWriter().write((String) result);
    }
}
